<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button class="post-btn" :disabled="!message" @click="onCommentPost"
      >发布</van-button
    >
  </div>
</template>

<script>
import { CommentPost } from '@/api/comment.js'
export default {
  name: 'CommentPost',
  props: {
    // 目标ID 文章ID或评论ID
    target: {
      type: [String, Number, Object],
      required: true
    },
    artId: {
      type: [String, Number, Object],
      default: null
    }
  },
  data () {
    return {
      message: ''
    }
  },
  methods: {
    // 发布评论
    async onCommentPost () {
      try {
        const { data: { data: { new_obj: res } } } = await CommentPost({
          target: this.target + '',
          content: this.message,
          art_id: this.artId ? (this.artId + '') : this.artId
        })
        console.log(res)
        this.$emit('onCommentPost', res)
        this.$toast.success('评论成功')
        this.message = ''
      } catch (err) {
        this.$toast.success('评论失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
